import java.io.*;
import java.beans.*;

public class Customer implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4998830261181655162L;
	protected int moneyLeft;
	protected Salesman mySalesman;
	protected Bank myBank;
	protected VetoableChangeSupport vetoCS;

	public Customer() {
		moneyLeft = 7;
		vetoCS = new VetoableChangeSupport(this);
		// not too tight, like
		// myBank = (Bank)Helper.newBean("Bank");
		// myBank.setCustomer(this);

		System.out.println("Customer: Neuer Customer erstellt");
	};

	public void setMoneyLeft(int m) {
		System.out.println("Customer: moneyLeft" + m);
		moneyLeft = m;
	}

	public int getMoneyLeft() {
		System.out.println("Customer: getMoneyLeft");
		return moneyLeft;
	}

	public Bank getBank() {
		System.out.println("Customer: getBank");
		return myBank;
	}

	public void setBank(Bank b) {
		System.out.println("Customer: setBank");
		myBank = b;
	}

	public Salesman getMySalesman() {
		System.out.println("Customer: getMySalesman");
		return mySalesman;
	}

	public void setMySalesman(Salesman sm) {
		System.out.println("Customer: setMySalesman");
		mySalesman = sm;
		sm.setBusy(true);
	}

	protected void pay(int money) throws PropertyVetoException {
		if (money > moneyLeft) {
			// not enough cash, what about a credit
			System.out
					.println("Customer: pay - not enough cash, what about a credit "
							+ money);
			vetoCS.fireVetoableChange("moneyLeft", new Integer(moneyLeft),
					new Integer(money));
			moneyLeft = 0;
		} else {
			System.out.println("Customer: pay " + money);
			moneyLeft = moneyLeft - money;
		}
	}

	public void addVetoableChangeListener(VetoableChangeListener vl) {
		vetoCS.addVetoableChangeListener(vl);
	}

	public void removeVetoableChangeListener(VetoableChangeListener vl) {
		vetoCS.removeVetoableChangeListener(vl);
	}

	public void buy(String product) {
		int productPrice;

		if (mySalesman != null) {
			try {
				productPrice = mySalesman.askForProduct(product);
				System.out.println("Customer: buy " + product + " - "
						+ productPrice);
			} catch (NotAvailable e) {
				System.out.println("Salesman told me, " + product
						+ " not available since " + e);
				return;
			}
			mySalesman.sell(this);
		}
	}
}
